# from tinydb import TinyDB, Query,where
# db = TinyDB('tinydb.json')
# articleCol= db.table('articles')
# ls=articleCol.search(where('product_name')=='焦炭')
# for item in ls:
#     print(item['title'],item['url']) 
#product_name

# import math

# def ramanujan_pi(n):
#     sum = 0
#     for k in range(n):
#         sum += (math.factorial(4 * k) * (1103 + 26390 * k)) / (math.pow(math.factorial(k), 4) * math.pow(396, 4 * k))
#     return 1 / (2 * math.sqrt(2) / 9801 * sum)

# # 可以调整计算的迭代次数
# n = 5
# pi_approx = ramanujan_pi(n)
# print("近似圆周率:", pi_approx)

from volcengine.maas.v2 import MaasService

maas = MaasService('maas-api.ml-platform-cn-beijing.volces.com', 'cn-beijing')

maas.set_ak('AKLTN2Y4MzdhMDk0Y2NkNDdmMDkxZWY2Yzk5ZjM0MmRlNTk')

maas.set_sk('T1RFNU0yWTRORGczWldFek5ETTJOVGcyWWpSak1EWmhNRFl3T1dNek9EYw==')

req = {
    "stream": True,
    "parameters": {
        "max_new_tokens": 0,
        "temperature": 0.7,
    },
    "messages": [
                    {
                        "role": "user",
                      "content": "你好"
                    }
                ]
}
text = ""

for resp in maas.stream_chat("0", req):
    if resp.error == None:
        chunk = resp.choices[0].message.content
        text += chunk
        print({"error_code": 0, "text": text})
    else:
        data = {
            "error_code": 500,
            "text": f"请求方舟 API 时发生未知的错误: {resp}"
        }
        print(data)
        break